﻿// <copyright file="ConnectionProperties.cs" company="Sinclair Community College">
// Copyright (c) Sinclair Community College. All rights reserved.
// </copyright>

namespace SinclairCC.AppVSuite.Shared
{
    using System;
    using System.Windows.Forms;

    /// <summary>
    /// This class represents a form that lets users configure options for connecting to the App-V SQL database.
    /// </summary>
    public partial class ConnectionProperties : Form
    {
        /// <summary>
        /// Initializes a new instance of the ConnectionProperties class.
        /// </summary>
        public ConnectionProperties()
        {
            this.InitializeComponent();
        }

        /// <summary>
        /// Handles the CheckedChanged event for the authentication options radio buttons.
        /// </summary>
        /// <param name="sender">
        /// The radio button whose checked state has changed.
        /// </param>
        /// <param name="e">
        /// Data specific to this event.
        /// </param>
        private void AuthTypeRadioCheckChange(object sender, EventArgs e)
        {
            // Only enable the SQL user name and password text boxes if SQL authentication has been chosen.
            this.userNameTextBox.Enabled = this.sqlAuthRadio.Checked;
            this.userNameLabel.Enabled = this.userNameTextBox.Enabled;
            this.passwordTextBox.Enabled = this.userNameTextBox.Enabled;
            this.passwordLabel.Enabled = this.userNameTextBox.Enabled;
        }

        /// <summary>
        /// Handles the Click event for the OK button.
        /// </summary>
        /// <param name="sender">
        /// The button being clicked.
        /// </param>
        /// <param name="e">
        /// Data specific to this event.
        /// </param>
        private void ClickOKButton(object sender, EventArgs e)
        {
            // Save the chosen settings.
            Settings.ServerName = this.serverNameTextBox.Text.Trim();
            Settings.Database = this.databaseNameTextBox.Text.Trim();
            Settings.SqlUserName = this.userNameTextBox.Text.Trim();
            Settings.SqlPassword = this.passwordTextBox.Text.Trim();
            Settings.UseWindowsAuthentication = this.windowsAuthRadio.Checked;
        }

        /// <summary>
        /// Handles the Load event for the form.
        /// </summary>
        /// <param name="sender">
        /// The form being loaded.
        /// </param>
        /// <param name="e">
        /// Data specific to this event.
        /// </param>
        private void FormLoad(object sender, EventArgs e)
        {
            this.serverNameTextBox.Text = Settings.ServerName;
            this.databaseNameTextBox.Text = Settings.Database;
            this.userNameTextBox.Text = Settings.SqlUserName;
            this.passwordTextBox.Text = Settings.SqlPassword;
            if (Settings.UseWindowsAuthentication.HasValue)
            {
                this.windowsAuthRadio.Checked = Settings.UseWindowsAuthentication.Value;
                this.sqlAuthRadio.Checked = !this.windowsAuthRadio.Checked;
            }
            else
            {
                this.windowsAuthRadio.Checked = true;
            }
        }
    }
}
